/**
 * Created by Administrator on 2018-07-06.
 */
var jtopoUtil = {
    canvas:null,
    stage:null,
    scene:null,
    currentNode :null,
    alarmArray:[],
    init:function(id,bgimg){//id：画板ID，showToobar：true||false,bgimg:背景图片

        $("#"+id).attr("width",screen.width );
        $("#"+id).attr("height",screen.height ) ;

        this.canvas = this.canvas ||  document.getElementById(id);
        this.stage = this.stage || (new JTopo.Stage(this.canvas));

        //this.stage.eagleEye.visible = true;
        //this.stage.wheelZoom = 0.85;

        this.stage.clear();
        this.scene = this.scene || (new JTopo.Scene());
        this.scene.background = bgimg || '../appBord/bg.jpg';


        this.stage.click(function(event){
            var e = event || window.event;
            //this.currentNode = null;
            $("#contextmenu").hide();
        });

        this.addMenuEvent();
    },

    paint:function(showToobar){
        this.stage.add(this.scene);
        showToobar = showToobar || false;
        if(showToobar){
            showJTopoToobar(this.stage);
            //console.log("................................")
        }
    },

    addContainer:function(name,fillColor){
        var container = new JTopo.Container(name);
        container.textPosition = 'Top_Left';
        container.fillColor = fillColor || '0,0,0';
        container.fontColor = '255,255,255';
        container.font = '11pt 微软雅黑';
        container.borderColor = '255,255,255';
        container.borderRadius = 30; // 圆角
        this.scene.add(container);
        return container;
    },
    addNode:function(nodeNo,name,x, y, img,typeColor,container,width,height,action,fullScreen){
        var node = new JTopo.Node(name);
        //console.log(name,this.isImg(img))
        if(this.isImg(img)){
            node.setImage(img, true);
        }else{
            node.textPosition = 'Middle_Center';
            node.fillColor = typeColor || '225,225,225';
            node.borderWidth = 1; // 边框的宽度
            node.borderColor = '255,255,255'; //边框颜色
        }
        node.action = action || ''
        node.nodeNo = nodeNo
        width =  width || 0 ;
        height = height || 0;
        if(width !=0 && height != 0 ){
            node.width = width;
            node.height = height;
        }
        node.borderRadius = 5;
        node.setLocation(x, y);

        this.scene.add(node);
        if(container){
            container.add(node);
        }
        this.addEventForNode(node,fullScreen);
        return node;
    },
    addLink:function(nameA,nameB,relaLine){
        var nodeA = this.getNode(nameA);
        var nodeZ = this.getNode(nameB);
        relaLine = relaLine || "Link";
        var link;
        if(relaLine.indexOf("FoldLink") !=-1 ){
            link = new JTopo.FoldLink(nodeA, nodeZ);
        }else{
            link = new JTopo.Link(nodeA, nodeZ);
        }
        link.strokeColor = '255,255,0';
        if(relaLine == "FoldLink1"  && relaLine == "Link1"){
            link.arrowsRadius = 6; //箭头大小
        }
        link.lineWidth = 1; // 线宽
        link.bundleGap = 50; // 线条之间的间隔
        link.direction = 'vertical';
        this.scene.add(link);
        return link;
    },
    addAlarm:function(node,typeColor,alarmColor,alarmInfo,alarmFlash){
        //var node = this.getNode(nodeName);
        var alarmFlash = alarmFlash || 0;
        var typeColor = typeColor || null;
        var alarmColor = alarmColor || null;
        var alarmInfo = alarmInfo || null;

        if(alarmFlash > 0 && alarmInfo!=null && alarmInfo!= '' ){
           var al = setInterval(function(){
                if(node.alarm == alarmInfo  ){
                    node.alarm = null;
                    node.alarmColor = '' ;
                }else{
                    node.alarm = alarmInfo
                    node.alarmColor = alarmColor ;
                }
            }, alarmFlash);
            this.alarmArray.push(al)
        }
    },
    alarmClear:function(){
        $.each(this.alarmArray,function(index,item){
            clearInterval(item);
        })
        this.alarmArray = [];
        return this.alarmArray.length;
    },

    getNode:function(nodeName){
        var nodes = this.scene.findElements(function(e){
            return e.text == nodeName;
        });

        if(nodes.length > 0){
            return nodes[0]
        }else{
            return null
        }
    },

    addEventForNode:function(node,fullScreen) {
        if(fullScreen){
            node.dragable = false
        }
        node.mouseup(function (event) {
            var e = event || window.event;
            var nodeNo = node.text;
            //console.log(node,event);
            jtopoUtil.currentNode = node;
            if (e.button == 2 && !fullScreen) {// 右键
                // 当前位置弹出菜单（div）
                $("#contextmenu").css({
                    top: e.y,
                    left: e.x
                }).show();
            } else if (e.button == 0 && !fullScreen) {
                $.getJSON(basePath+"/appBordNodeMonitor/chLocation", {
                    nodeNo: nodeNo,
                    x: node.x,
                    y: node.y
                }, function (data) {
                    if (data.statusCode < 0) {
                        alert(data.message);
                    }
                })
            }
        });

        node.mouseover(function (event) {
            var e = event || window.event;
            var nodeNo = node.text;
            $("#appBordAlertInfo").empty();
            $.getJSON(basePath+"/appBordNodeMonitor/nodeDesc",
                {nodeNo: nodeNo},
                function (data) {
                    //console.log(data)
                    if (data.statusCode >= 0 && data.message !=null) {
                        $("#appBordAlertInfo").append(data.message).css({
                            top: e.y <100 ?(e.y +node.height/2 ):(e.y - node.height/2),
                            left:e.x <100? node.width+10 :(e.x-100)
                        }).show();
                    }
                })

        });
        node.mouseout(function (event) {
            var e = event || window.event;
            $("#appBordAlertInfo").hide();
        });

        node.dbclick(function (event) {
            var e = event || window.event;
            if(node.action != "" && node.action != null){
                layer.open({
                    type:2,
                    title:'<i class="layui-icon">&#xe66e;</i> '+node.text,
                    area:['100%','100%'],
                    content:node.action +"="+node.nodeNo
                })
            }
        });
    },
    addMenuEvent:function(){
        $("#contextmenu a").click(function(){
            var text = $(this).text();

            if(text == '删除该节点'){
                $.getJSON(basePath+"/appBordNodeMonitor/deleteNode",{nodeNo:jtopoUtil.currentNode.text},function(data){
                    if(data.statusCode < 0){
                        alert(data.message);
                    }else{
                        jtopoUtil.scene.remove(jtopoUtil.currentNode);
                        jtopoUtil.currentNode = null;
                    }
                })
            }if(text == '撤销上一次操作'){
                jtopoUtil.currentNode.restore();
            }else{
                jtopoUtil.currentNode.save();
            }

            if(text == '更改颜色'){
                jtopoUtil.currentNode.fillColor = JTopo.util.randomColor();
                console.log(JTopo.util.randomColor())
            }else if(text == '顺时针旋转'){
                jtopoUtil.currentNode.rotate += 0.5;
            }else if(text == '逆时针旋转'){
                jtopoUtil.currentNode.rotate -= 0.5;
            }else if(text == '放大'){
                //jtopoUtil.currentNode.scaleX += 0.2;
                //jtopoUtil.currentNode.scaleY += 0.2;
                jtopoUtil.currentNode.width = jtopoUtil.currentNode.width + 12
                jtopoUtil.currentNode.height = jtopoUtil.currentNode.height + 12

                $.getJSON(basePath+"/appBordNodeMonitor/updateNode",{nodeNo:jtopoUtil.currentNode.text,width:jtopoUtil.currentNode.width ,height:jtopoUtil.currentNode.height },function(data){
                    if(data.statusCode < 0){
                        alert(data.message);
                    }
                })
            }else if(text == '缩小'){
               //jtopoUtil.currentNode.scaleX -= 0.2;
                //jtopoUtil.currentNode.scaleY -= 0.2;
                jtopoUtil.currentNode.width = (jtopoUtil.currentNode.width - 12)<0?10:(jtopoUtil.currentNode.width - 12)
                jtopoUtil.currentNode.height = (jtopoUtil.currentNode.height -  12)<0?10:(jtopoUtil.currentNode.height -  12)

                $.getJSON(basePath+"/appBordNodeMonitor/updateNode",{nodeNo:jtopoUtil.currentNode.text,width:jtopoUtil.currentNode.width ,height:jtopoUtil.currentNode.height },function(data){
                    if(data.statusCode < 0){
                        alert(data.message);
                    }
                })
            }else if(text == '横向拉伸'){
                //jtopoUtil.currentNode.scaleX -= 0.2;
                //jtopoUtil.currentNode.scaleY -= 0.2;
                jtopoUtil.currentNode.width = jtopoUtil.currentNode.width + 12*2
                //jtopoUtil.currentNode.height = (jtopoUtil.currentNode.height -  12*2)<0?10:(jtopoUtil.currentNode.height -  12*2)

                $.getJSON(basePath+"/appBordNodeMonitor/updateNode",{nodeNo:jtopoUtil.currentNode.text,width:jtopoUtil.currentNode.width ,height:jtopoUtil.currentNode.height },function(data){
                    if(data.statusCode < 0){
                        alert(data.message);
                    }
                })
            }else if(text == '竖向拉伸'){
                //jtopoUtil.currentNode.scaleX -= 0.2;
                //jtopoUtil.currentNode.scaleY -= 0.2;
                //jtopoUtil.currentNode.width = (jtopoUtil.currentNode.width - 12*2)<0?10:(jtopoUtil.currentNode.width - 12*2)
                jtopoUtil.currentNode.height = jtopoUtil.currentNode.height +  12*2

                $.getJSON(basePath+"/appBordNodeMonitor/updateNode",{nodeNo:jtopoUtil.currentNode.text,width:jtopoUtil.currentNode.width ,height:jtopoUtil.currentNode.height },function(data){
                    if(data.statusCode < 0){
                        alert(data.message);
                    }
                })
            }
            //console.log(jtopoUtil.currentNode.width+","+jtopoUtil.currentNode.height)
            $("#contextmenu").hide();
        });
    },

    imgJudge : [".png",".img",".gif",".bmp",".jpg",".jpeg"],
    isImg:function(pathImg){
        var str = pathImg.substring(pathImg.lastIndexOf("."));
        //console.log("str:" + str);
        if(str.length > 0 ){
            str = str.toLowerCase();
            if($.inArray(str, this.imgJudge) !=-1){
                return true
            }
        }
       return false
    },
    addLegend:function(name,x,y,typeColor){
        var node = new JTopo.Node(name);
        node.alpha = 0.7;
        node.fillColor = typeColor||'';
        node.setLocation(screen.width-x, screen.height-y);
        node.textPosition = 'Middle_Center'
        //node.textOffsetY = 3;
        node.font = '12px 微软雅黑';
        node.width = 60;
        node.showSelected = false; // 不显示选中矩形
        node.borderRadius = 5;
        node.dragable = false;
        jtopoUtil.scene.add(node);
    }
}



/**
 findElements(function（e）{return true})  筛选,true为筛选通过
 selectElements  属性（被选中的元素）
 getElementByXY（x,y） 获取屏幕当前x,y的元素
 **/

/**
 <ul id="contextmenu" style="display:none;">
 <li><a>顺时针旋转</a></li>
 <li><a>逆时针旋转</a></li>
 <li><a>更改颜色</a></li>
 <li><a>放大</a></li>
 <li><a>缩小</a></li>
 <li><a>删除该节点</a></li>
 </ul>
 **/

